package com.winai.launcher.view;

import android.annotation.SuppressLint;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ProgressBar;

import com.ks.projectbasictools.utils.LogUtils;
import com.winai.launcher.R;
import com.winai.launcher.base.BaseActivity;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class WebViewActivity extends BaseActivity {
    private String url;

    @BindView(R.id.wv_webView)
    WebView wv_webView;
    @BindView(R.id.prog)
    ProgressBar progressBar;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_web_view);
        ButterKnife.bind(this);
        initObject();
        initView();
    }

    private void initObject() {
        url = getIntent().getStringExtra("url");
    }

    @SuppressLint("SetJavaScriptEnabled")
    private void initView() {
        //启用支持JavaScript
        wv_webView.getSettings().setJavaScriptEnabled(true);
        wv_webView.loadUrl(url);
        //设置可自由缩放网页
        wv_webView.getSettings().setSupportZoom(true);
        wv_webView.getSettings().setBuiltInZoomControls(true);
        //覆盖WebView默认使用第三方或系统默认浏览器打开网页的行为，使网页用WebView打开
        wv_webView.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                view.loadUrl(url);
                return true;
            }
        });
        //设置WebChromeClient类
        wv_webView.setWebChromeClient(new WebChromeClient() {
            //获取网站标题
            @Override
            public void onReceivedTitle(WebView view, String title) {
                LogUtils.d("该html页面标题:"+title);//html中的title值
            }
            //获取加载进度
            @Override
            public void onProgressChanged(WebView view, int newProgress) {
                if (newProgress == 100) {
                    // 网页加载完成
                    progressBar.setVisibility(View.GONE);//加载完网页进度条消失
                } else {
                    // 加载中
                    progressBar.setVisibility(View.VISIBLE);//开始加载网页时显示进度条
                    progressBar.setProgress(newProgress);//设置进度值
                }
            }
        });


        //设置WebViewClient类
        wv_webView.setWebViewClient(new WebViewClient() {
            //设置加载前的函数
            @Override
            public void onPageStarted(WebView view, String url, Bitmap favicon) {
                LogUtils.d("html页面开始加载。");
            }

            //设置结束加载函数
            @Override
            public void onPageFinished(WebView view, String url) {
                LogUtils.d("html页面结束加载。");
            }
        });
    }

    //go back
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        // Check if the key event was the Back button and if there's history
        if ((keyCode == KeyEvent.KEYCODE_BACK) && wv_webView.canGoBack()) {
            wv_webView.goBack();
            return true;
        }
        //  return true;
        // If it wasn't the Back key or there's no web page history, bubble up to the default
        // system behavior (probably exit the activity)
        return super.onKeyDown(keyCode, event);
    }

    @OnClick({R.id.tv_close, R.id.tv_back})
    void onClick(View view) {
        switch (view.getId()) {
            case R.id.tv_back:
                if (wv_webView.canGoBack()) {
                    wv_webView.goBack();
                }
                break;
            case R.id.tv_close:
                this.finish();
                break;
        }
    }
}
